package com.common.app.dialog

import android.annotation.SuppressLint
import android.app.Dialog
import android.content.Context
import android.graphics.Bitmap
import android.graphics.drawable.ColorDrawable
import android.os.Build
import android.os.Bundle
import android.view.*
import com.common.app.R
import com.common.app.data.bean.ShareInfo
import com.common.app.databinding.DialogCommonShareBinding
import com.common.app.utls.share.ShareHelper
import com.common.base.app.extras.setOnClickListener
import com.common.base.utils.BitmapUtils
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
import io.reactivex.rxjava3.core.Observable
import io.reactivex.rxjava3.schedulers.Schedulers
import java.util.concurrent.TimeUnit


/**
 *
 * 作者：sosou
 *
 * 版本：1.0
 *
 * 创建日期：2023/2/18
 *
 * 描述：通用分享弹框
 *
 * 修订历史：
 *
 */
class CommonShareDialog(private var mContext: Context) :
    Dialog(mContext, R.style.custom_dialog_sv), View.OnClickListener {

    private var shareInfo: ShareInfo? = null

    private var binding: DialogCommonShareBinding? = null

    private var listener: OnCommonShareDialogSaveListener? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = DialogCommonShareBinding.inflate(LayoutInflater.from(context))
        binding?.root?.let { setContentView(it) }
        initView()
    }

    fun show(shareInfo: ShareInfo) {
        show()

        this.shareInfo = shareInfo
        shareInfo.wxAppId?.let { ShareHelper.initShare(it) }
    }

    override fun show() {
        super.show()
        window?.apply {
            //设置宽度
            attributes.width = ViewGroup.LayoutParams.MATCH_PARENT
            attributes.gravity = Gravity.BOTTOM //设置dialog 在布局中的位置
            setWindowAnimations(R.style.bottom_style_dialog) //添加动画:
            setBackgroundDrawable(ColorDrawable(mContext.getColor(R.color.transparent))) // android:windowBackground
        }
    }

    /**
     * 初始化控件
     */
    private fun initView() {
        binding?.tvClose?.setOnClickListener(onClick = this)
        binding?.layoutAlbum?.setOnClickListener(onClick = this)
    }

    override fun onClick(v: View) {
        binding?.apply {
            when (v) {
                tvClose -> dismiss()
                layoutAlbum -> {
                    listener?.onShareDialogSaveClick(null)
                }
            }
        }

    }


    /**
     * @param listener
     */
    fun setListener(listener: OnCommonShareDialogSaveListener?) {
        this.listener = listener
    }

    /**
     *
     */
    interface OnCommonShareDialogSaveListener {
        fun onShareDialogSaveClick(mBmp: Bitmap?)
    }
}